﻿import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;


public class CourseDao {

	public static ArrayList<Course> queryCourseList() {
		 ArrayList<Course> courseList = new ArrayList<>();
		/**
		 * 查询课程列表
		 */
			Connection conn = ConnectionUtil.getConnection();
	        Statement stmt = null;
	        try{
	           
	            // 执行查询
	            System.out.println(" 实例化Statement对象...");
	            stmt = conn.createStatement();
	            String sql;
	            
	            sql = "SELECT kch, kcm, ls, zy FROM course ";
	            ResultSet rs =  stmt.executeQuery(sql);
	        
	            // 展开结果集数据库
	            while(rs.next()){
	                // 通过字段检索
	                int kch  = rs.getInt("kch");
	                String kcm = rs.getString("kcm");
	                String ls = rs.getString("ls");
	                String zy = rs.getString("zy");
	                
	                Course course = new Course(kch, kcm,ls,zy);
					courseList.add(course);
	            }
	     /*//完成后关闭
            rs.close();
            stmt.close();*/
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }/*finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }*/
	        System.out.println("查询成功！");
	        return courseList;	
	}

	public static void addUsePrepareStatement(Course course) {
		Connection conn=ConnectionUtil.getConnection();
		String sql="insert into course values(?,?,?,?)";
		try {
			java.sql.PreparedStatement psta = conn.prepareStatement(sql);
			psta.setInt(1, course.getKch());
			psta.setString(2, course.getKcm());
			psta.setString(3, course.getLs());
			psta.setString(4, course.getZy());

			int result = psta.executeUpdate();
			if(result > 0) {
				System.out.println("插入成功");
			}else {
				System.out.println("插入失败");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		*/
		
	}

	public static void del(String kcm) {
		Connection conn = ConnectionUtil.getConnection();

		String sql = "delete from course where kcm = '"+kcm+"'";
		
		try {
			Statement stat = conn.createStatement();
			int result = stat.executeUpdate(sql);
			if(result > 0) {
				System.out.println("删除成功");
			}else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void update1(String kcm1, int column1, String value1) {
		Connection conn =ConnectionUtil .getConnection();
		String sql=null;
		switch(column1){
		case 1:
			sql="update course Set kcm='"+value1+"' where kcm='"+kcm1+"'";
			break;
		case 2:
			sql="update course Set kch='"+value1+"' where kcm='"+kcm1+"'";
			break;
		case 3:
			sql="update course Set ls='"+value1+"' where kcm='"+kcm1+"'";
			break;
		default:
			break;
			
		}
		
		try {
			Statement stat = conn.createStatement();
			int result1 =stat.executeUpdate(sql);
			if(result1>0){
				System.out.println("更新成功");
			}else{
				System.out.println("更新失败");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	
}

